package com.lottery.im.module.result.ui.statistics;

import android.os.Bundle;
import android.widget.GridView;
import butterknife.BindView;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.LongStatisticsInfo;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.constant.RxBusCode;
import com.lottery.im.module.result.adapter.result.statistics.LongStatisticsAdapter;
import com.lottery.im.module.result.presenter.DoubleStatisticsPresenter;
import com.lottery.im.module.result.view.DoubleStatisticsView;
import com.lottery.im.util.Utils;
import com.lottery.im.util.rxbus.RxBus;
import com.lottery.im.util.rxbus.Subscribe;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 长龙统计
 */
public class LongStatisticsFragment extends BaseSwipeFragment<DoubleStatisticsPresenter>
    implements DoubleStatisticsView {
  private final static String GAME_TYPE_KEY = "GAME_TYPE_KEY";

  public static LongStatisticsFragment create(String gameType) {
    LongStatisticsFragment fragment = new LongStatisticsFragment();
    Bundle b = new Bundle();
    b.putString(GAME_TYPE_KEY, gameType);
    fragment.setArguments(b);
    return fragment;
  }

  private String[] ballNameArr =
      { "", "", "", "第1球", "第2球", "第3球", "第4球", "第5球", "第6球", "第7球", "第8球", "第9球", "第10球" };
  private String[] ballTypeArr =
      { "大", "小", "单", "双", "龙", "虎", "和" };
  @BindView(R.id.long_grid_view) GridView mGridView;
  private List<LongStatisticsInfo> mLongList = new ArrayList<>();
  private LongStatisticsAdapter mAdapter;
  private String mGameType;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_statistics_long;
  }

  @Override protected void initPresenter() {
    presenter = new DoubleStatisticsPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    if (getArguments() == null) {
      popBackStack();
      return;
    } else {
      RxBus.get().register(this);
      mGameType = getArguments().getString(GAME_TYPE_KEY);
      mAdapter = new LongStatisticsAdapter(getActivity(), mLongList);
      mGridView.setAdapter(mAdapter);
      presenter.queryDXDSLost(mGameType, "2");
    }
  }

  @Override public void queryDxDsSuccess(List<String> list) {
    for (int i = 0; i < list.size(); i++) {
      String string = list.get(i);
      if (i > 2) {
        String[] str = string.split(",");
        if (str.length > 0) {
          for (int j = 0; j < str.length; j++) {
            if (Utils.isInteger(str[j])) {
              int a = Integer.parseInt(str[j]);
              if (a > 1) {
                LongStatisticsInfo info =
                    new LongStatisticsInfo(ballNameArr[i], ballTypeArr[j], a);
                mLongList.add(info);
              }
            }
          }
        }
      }
    }
    Collections.sort(mLongList, new SortByNum());
    mAdapter.notifyDataSetChanged();
  }

  class SortByNum implements Comparator {//按期数从大到小排序

    public int compare(Object o1, Object o2) {
      LongStatisticsInfo s1 = (LongStatisticsInfo) o1;
      LongStatisticsInfo s2 = (LongStatisticsInfo) o2;
      if (s1.getGameNum() <= s2.getGameNum()) {
        return 1;
      }
      return -1;
    }
  }

  /*
   * 刷新开奖结果结果
   * */
  @Subscribe(code = RxBusCode.REFRESH_CODE)
  public void refresh() {
    if (null == mActivity || mActivity.isFinishing()) {
      return;
    }
    presenter.queryDXDSLost(mGameType, "2");
  }

  @Override public void onDestroy() {
    super.onDestroy();
    RxBus.get().unRegister(this);
  }
}
